/*******************************************************************
 * Author	: Harsh and Maulik
 * Description	: This function will be only used in parsig the boolean
 *		  function and putting them in an array to give index.
 * 		  A better implementation of parse can be made with a 
 *		  single loop.
 * Last updated	: 1st Dec, 2011
 *******************************************************************/



#include "BDD.h"

//extern int totVar;
extern int id;



/* This function will check whether given variable is in array or not.
 * If not it will enter it else wont do anything
 */

int checkIndex(char *tempString, char **variables, graph *g)
{
	int i=0;
	char *string = (char *) malloc (sizeof(strlen(tempString)));
	strcpy(string,tempString);
	while(i <1000)
	{
		if( variables[i] == NULL){
			variables[i] = string; 
			(g->totVar)++;
			break;
		} else {
			if(!(strcmp(string,variables[i])))		
				break;
		}
		i++;
	}
	return (i+1);
}


//This function will parse the string and give you individual variables. Which I will send to check variables to see whether they have been parsed before or not.

void parse(char *c, char **variables, graph *g)
{
	int index=0;
	char tempString[1000];

	while(*c !='\0')
	{
		if((*c != '.') && (*c != '+') && (*c != '~'))
		{
			tempString[index++]=*c;
		} else {
			tempString[index++]='\0';
			if(strlen(tempString))
				checkIndex(tempString,variables,g);
			index = 0;
		}
		c++;
	}
	tempString[index]='\0';
	if(strlen(tempString))
		checkIndex(tempString,variables,g);
	index = 0;
	
}



void initVariables(char *c,char **variables, graph *g)
{
	int i= 0;
	for(;i<1000;i++)
		variables[i] = NULL;
	
	(g->totVar) = 0;
	parse(c, variables, g);



}

void deInitVariables(char **variables)
{
	int i = 0;
	while(variables[i]!= NULL)
		free(variables[i++]);
}
